package edu.its.dreamteam.web.pages.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.BuyPlayer;
import edu.its.dreamteam.daf.beans.Club;
import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.Player;
import edu.its.dreamteam.daf.beans.SellPlayer;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IBuyPlayerDao;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.ISellPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SecureBasePage;
import edu.its.dreamteam.web.pages.entity.ClubPlayer;

public class TransferPlayer extends SecureBasePage{
	@Inject
	private ITeamDao teamDao;		
	@Inject
	private ISellPlayerDao sellPlayerDao;
	@Inject
	private IBuyPlayerDao buyPlayerDao;
	@Inject
	private IDraftPlayerDao draftPlayerDao;
	
	private List<SellPlayer> sellPlayerList;
	private List<BuyPlayer> buyPlayerList;
	
	private BuyPlayer buyPlayer;
	private SellPlayer sellPlayer;
	private Team team;
	
	@Retain
	private BeanModel sellPlayerModel;
	@Retain
	private BeanModel buyPlayerModel;
	@Inject
	private BeanModelSource beanModelSource;
    @Inject
    private ComponentResources resources;
    
    private int bidPlayer;
    
    
	public int getBidPlayer() {
		return bidPlayer;
	}

	public void setBidPlayer(int bidPlayer) {
		this.bidPlayer = bidPlayer;
	}

	public SellPlayer getSellPlayer() {
		return sellPlayer;
	}

	public void setSellPlayer(SellPlayer sellPlayer) {
		this.sellPlayer = sellPlayer;
	}
	
	public BuyPlayer getBuyPlayer() {
		return buyPlayer;
	}

	public void setBuyPlayer(BuyPlayer buyPlayer) {
		this.buyPlayer = buyPlayer;
	}

	public ITeamDao getTeamDao() {
		return teamDao;
	}
		
	public ISellPlayerDao getSellPlayerDao() {
		return sellPlayerDao;
	}
	
	public IBuyPlayerDao getBuyPlayerDao() {
		return buyPlayerDao;
	}
	
	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}

	public Team getTeam() {
		if(team==null){
			team = (Team) getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return team;
	}	
	
	public List<SellPlayer> getSellPlayerList() {
		if(sellPlayerList==null){
			sellPlayerList = new ArrayList<SellPlayer>();
			sellPlayerList = getSellPlayerDao().getSellPlayerNotTeam(getTeam().getIdTeam(),getTeam().getIdRoom());
		}
		return sellPlayerList;
	}	
	
	public List<BuyPlayer> getBuyPlayerList() {
		if(buyPlayerList==null){
			buyPlayerList = new ArrayList<BuyPlayer>();
			buyPlayerList = getBuyPlayerDao().getBuyPlayerNotTeam(getTeam().getIdTeam(), getTeam().getIdRoom());
		}
		return buyPlayerList;
	}

	public BeanModel getSellPlayerModel() {
		return sellPlayerModel;
	}
	
	public BeanModel getBuyPlayerModel() {
		return buyPlayerModel;
	}

	void pageLoaded(){    	
    	
    	sellPlayerModel = beanModelSource.create(SellPlayer.class, false, resources);    
    	sellPlayerModel.remove("idDraftPlayer","idSell","status");
    	sellPlayerModel.add("player",null).label("Player");
    	sellPlayerModel.add("club",null).label("Club");
    	sellPlayerModel.add("position",null).label("Position");
    	sellPlayerModel.add("bid",null).label("Price");
    	sellPlayerModel.add("actions", null).label("Sell");
    	sellPlayerModel.reorder("player","club","position");
    	
    	buyPlayerModel = beanModelSource.create(BuyPlayer.class, false, resources);
    
	}
	
	
	void onSuccessFromSellPlayerForm(String idSell){
		SellPlayer sellPlayerManager = (SellPlayer)getSellPlayerDao().findByPrimaryKey(idSell);	
		BuyPlayer buyPlayerManager = (BuyPlayer)getBuyPlayerDao().queryForBaseBean("\"id_sell\" = '"+idSell+"' ");
		
		if(buyPlayerManager!=null){
			if(getBidPlayer()>buyPlayerManager.getBid()){
				buyPlayerManager.setIdTeam(getTeam().getIdTeam());
				buyPlayerManager.setBid(getBidPlayer());
				getBuyPlayerDao().update(buyPlayerManager);
			}
		}else{
			BuyPlayer buyPlayerObj = new BuyPlayer();
			buyPlayerObj.setIdSell(idSell);
			System.out.println(getTeam().getIdTeam());
			buyPlayerObj.setIdTeam(getTeam().getIdTeam());
			buyPlayerObj.setStatus("0");
			buyPlayerObj.setDateBuy(new Date());
			buyPlayerObj.setBid(getBidPlayer());
			getBuyPlayerDao().insert(buyPlayerObj);
		}		
		
	}
}
